home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT62.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  10.9 KB  |  458 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 62                         
  5.                                           
  6.   Yet another scrolling example to demonstrate some simple techniques.        
  7.   The program uses rather small sprites and allows the user to configure      
  8.   window size. Frame rates are calculated and displayed at the end of the     
  9.   program. Elevators are available by pressing UP ARROW when standing on one. 
  10.                                           
  11.   ▒▒▒ PROJECT ▒▒▒                                                             
  12.   This program requires the WGT5_WC.LIB and WSCR_WC.LIB files to be linked.   
  13.                                           
  14.   ▒▒▒ DATA FILES ▒▒▒                                                          
  15.   MUN.WMP, MUNCHMAP.SPR, MUNCHKIN.SPR                                         
  16.                                WATCOM C++ VERSION 
  17. ==============================================================================
  18. */
  19. #include <dos.h>
  20. #include <malloc.h>
  21. #include <stdio.h>
  22. #include <conio.h>
  23.  
  24. #include <wgt5.h>
  25. #include <wgtscrol.h>
  26.  
  27. typedef short tiletypes[256];
  28.  
  29. #define YOU 37
  30. void checkfeet (void);
  31. void checkhead (short);
  32. void checkright (void);
  33. void checkleft (void);
  34. void findelevators (void);
  35. void upelev (void);
  36. void downelev (void);
  37. void checkelevators (void);
  38. void moveguys (void);
  39.  
  40. short ox,oy,dir,anim;
  41. short jumping,addy;
  42. short spx,spy;
  43.  
  44. int timer;
  45. // The number of times the screen is updated
  46. long updates;
  47.  
  48. short feet1, feet2, head1, head2;
  49. short windx, windy;
  50.  
  51. wgtmap munchmap;                        // our world map
  52. tiletypes munchtypes;
  53.  
  54. #define UP 72
  55. #define DOWN 80
  56. #define LEFT 75
  57. #define RIGHT 77
  58. #define CTRL 29
  59. #define ESC 1
  60.  
  61. #define NUM_SPR 51
  62. #define NUM_TILE 255
  63. #define NUM_OBJ 101
  64. #define TILE_SIZE 16
  65.  
  66. #define MAINWIN 0
  67.  
  68. color palette[256];             // our palette of colours
  69.  
  70. block munchtiles[NUM_TILE];             // our blocks for the map
  71. block munchsprites[NUM_SPR];            // our sprites 
  72. scrollsprite munchobj[NUM_OBJ];
  73.  
  74. short i;
  75.  
  76. typedef struct {
  77.   short curheight;
  78.   short origy, origx;
  79.   short timer;
  80. } elevator;
  81.  
  82. short replace[200];
  83. short elevup = -1;
  84. short oldmode;
  85.  
  86. elevator elev[30];
  87. short numelev = 0;
  88.  
  89.  
  90. void timerctr (void)
  91. {
  92.   timer++;
  93. }
  94.  
  95.  
  96. void main (void)
  97. {
  98.   oldmode = wgetmode ();
  99.   if (!vgadetected ())
  100.   {
  101.     printf ("VGA is required to run this program...");
  102.     exit (1);
  103.   }
  104.  
  105.   printf ("WGT Example #62\n\n");
  106.   printf ("8-WAY SCROLLING DEMO\n");
  107.   printf ("Arrow keys move, CTRL jumps. Up/down looks in direction or operates\n");
  108.   printf ("elevators.  ESC quits the program.\n");
  109.   printf ("\n\nWindow Width (2-20):");
  110.   scanf ("%i", &windx);
  111.   printf ("\nWindow Height (2-12):");
  112.   scanf ("%i", &windy);
  113.  
  114.   vga256 ();
  115.   wloadsprites (palette, "munchmap.spr", munchtiles, 0, NUM_TILE - 1);
  116.   wloadsprites (palette, "munchkin.spr", munchsprites, 0, NUM_SPR - 1);
  117.   wsetpalette (0, 255, palette);
  118.   
  119.   winitscroll (MAINWIN, NORMAL, - 1, windx, windy, munchtiles);
  120.   memset (&munchobj[0], 0, sizeof (scrollsprite)*NUM_OBJ);
  121.   munchmap = wloadmap (MAINWIN, "mun.wmp", munchtypes, munchobj);
  122.   
  123.   findelevators ();
  124.   
  125.   wnormscreen ();
  126.   wcls (0);
  127.   
  128.   wshowwindow (MAINWIN, 0, 0);
  129.   
  130.   installkbd ();
  131.   
  132.   munchobj[YOU].on = 1;
  133.   munchobj[YOU].x = 16;
  134.   munchobj[YOU].y = 242;
  135.   munchobj[YOU].num = 1;
  136.   
  137.   jumping = 0; addy = 0;
  138.   anim = 2;
  139.   
  140.   timer = 0;
  141.   winittimer ();
  142.   wstarttimer (timerctr, TICKS(100));
  143.   do {
  144.     spx = 0;
  145.     spy = 0;
  146.     ox = munchobj[YOU].x;
  147.     oy = munchobj[YOU].y;
  148.     
  149.     if (jumping == 1)
  150.       addy += 2;
  151.     if (addy > 15)
  152.       addy = 15;
  153.     
  154.     if ((kbdon[CTRL]) && (jumping == 0))
  155.     {
  156.       jumping = 1;
  157.       addy = - 14;
  158.     }
  159.     
  160.     if (kbdon[LEFT])
  161.     {
  162.       munchobj[YOU].x -= 8;
  163.       checkleft ();
  164.       if (dir != 1)
  165.       {
  166.     dir = 1;
  167.     anim = 5;
  168.       }
  169.       anim++;
  170.       if (anim > 8)
  171.     anim = 5;
  172.     }
  173.     else if (kbdon[RIGHT])
  174.     {
  175.       munchobj[YOU].x += 8;
  176.       checkright ();
  177.       if (dir != 2)
  178.       {
  179.     dir = 2;
  180.     anim = 1;
  181.       }
  182.       anim++;
  183.       if (anim > 4)
  184.     anim = 1;
  185.     }
  186.     
  187.     munchobj[YOU].num = anim;
  188.     if (munchobj[YOU].x == ox)
  189.       if (dir == 1)
  190.     munchobj[YOU].num = 9;
  191.     else munchobj[YOU].num = 1;
  192.     
  193.     munchobj[YOU].y += addy;
  194.     if (munchobj[YOU].y < 0) 
  195.       munchobj[YOU].y = 0;
  196.     if (addy < 0)
  197.       checkhead (0);
  198.     
  199.     
  200.     if ((jumping == 1))
  201.       if (dir == 1)
  202.     munchobj[YOU].num = 6;
  203.     else munchobj[YOU].num = 2;
  204.     
  205.     checkfeet ();
  206.     
  207.     spx = munchobj[YOU].x - worldx[MAINWIN] - windowmaxx[MAINWIN] / 2 - 1;
  208.     spy = munchobj[YOU].y - worldy[MAINWIN] - windowmaxy[MAINWIN] / 2 - 1;
  209.     
  210.     if (kbdon[UP])
  211.     {
  212.       if ((feet1 == 105) || (feet2 == 105))
  213.     upelev ();
  214.       else
  215.     spy = - 4;
  216.     }
  217.     if (kbdon[DOWN])
  218.     {
  219.       if ((feet1 == 105) || (feet2 == 105))
  220.     downelev ();
  221.       else
  222.     spy = + 4;
  223.     }
  224.     
  225.     checkelevators ();
  226.     // make sure they come back down when not standing on them
  227.     
  228.     moveguys ();
  229.     
  230.     wscrollwindow (MAINWIN, spx, spy);
  231.     wshowobjects (MAINWIN, 1, NUM_OBJ - 1, munchsprites, munchobj);
  232.     
  233.     wputblock (0, 0, scrollblock[MAINWIN], NORMAL);
  234.     nosound ();
  235.     updates++;
  236.   } while  (kbdon[ESC] != 1);          /* until ESC key is pressed */
  237.   wstoptimer ();  
  238.   wdonetimer ();
  239.  
  240.   uninstallkbd ();
  241.   
  242.   wendscroll (MAINWIN);
  243.   wfreesprites (munchtiles, 0, NUM_TILE - 1);
  244.   wfreesprites (munchsprites, 0, NUM_SPR - 1);
  245.   wfreemap (munchmap);
  246.   wsetmode (oldmode);
  247.   printf ("\nElapsed time (microseconds): %d", timer);
  248.   printf ("\n# updates: %d", updates);
  249.   printf ("\nMicroseconds per frame: %g", (float)(timer) / (float)(updates));
  250.   printf ("\nAverage frame rate: %2.2f frames/sec\n", (float)updates / ((float)(timer) / 100.0));
  251. }
  252.  
  253.  
  254. void checkright (void)
  255. {
  256.   short j,k;
  257.   short x;
  258.     
  259.   j = wgetworldblock (MAINWIN, munchobj[YOU].x + 16, munchobj[YOU].y + 1);
  260.   k = wgetworldblock (MAINWIN, munchobj[YOU].x + 16, munchobj[YOU].y + 15);
  261.   if  ((j >= 100) | (k >= 100))
  262.   {
  263.     x = munchobj[YOU].x / TILE_SIZE;
  264.     munchobj[YOU].x = x * TILE_SIZE;
  265.   }
  266. }
  267.  
  268.  
  269. void checkleft (void)
  270. {
  271.   short j, k;
  272.   short x;
  273.     
  274.   j = wgetworldblock (MAINWIN, munchobj[YOU].x, munchobj[YOU].y);
  275.   k = wgetworldblock (MAINWIN, munchobj[YOU].x, munchobj[YOU].y + 15);
  276.   if  ((j >= 100) || (k >= 100))
  277.   {
  278.     x = munchobj[YOU].x / TILE_SIZE;
  279.     x++;
  280.     munchobj[YOU].x = x * TILE_SIZE;
  281.   }
  282. }
  283.  
  284.  
  285. void checkfeet (void)
  286. {
  287.   short j, k, y;
  288.   
  289.   feet1 = wgetworldblock (MAINWIN, munchobj[YOU].x, munchobj[YOU].y + 16);
  290.   feet2 = wgetworldblock (MAINWIN, munchobj[YOU].x + 15, munchobj[YOU].y + 16);
  291.   if  ((feet1 < 50) && (feet2 < 50))
  292.     jumping = 1;
  293.   else 
  294.   {
  295.     y = munchobj[YOU].y / TILE_SIZE;
  296.     munchobj[YOU].y = y * TILE_SIZE;
  297.  
  298.     jumping = 0;
  299.     addy = 0;
  300.   }
  301. }
  302.  
  303.  
  304. void checkhead (short usingelev)
  305. {
  306.   short j, k, y;
  307.   
  308.   head1 = wgetworldblock (MAINWIN, munchobj[YOU].x, munchobj[YOU].y - 1);
  309.   head2 = wgetworldblock (MAINWIN, munchobj[YOU].x + 15, munchobj[YOU].y - 1);
  310.   if  ((head1 < 50) && (head2 < 50))
  311.     jumping = 1;
  312.   else 
  313.   {
  314.     y = munchobj[YOU].y  / TILE_SIZE;
  315.  
  316.     if (!usingelev)
  317.        y++;
  318.     munchobj[YOU].y = y * TILE_SIZE;
  319.  
  320.     jumping = 0;
  321.     addy = 0;
  322.   }
  323. }
  324.  
  325.  
  326. void findelevators (void)
  327. {
  328.   short i, j, k;
  329.   for  (i = 0; i <= mapheight[MAINWIN]; i++)
  330.     for  (j = 0; j <= mapwidth[MAINWIN]; j++)
  331.   {
  332.     k = wgetworldblock (MAINWIN, j*16, i*16);
  333.     if  (k == 105)
  334.     {
  335.       elev[numelev].curheight = i;
  336.       elev[numelev].origx = j;
  337.       elev[numelev].origy = i;
  338.       elev[numelev].timer = 0;
  339.       
  340.       for  (k = 0; k < 200; k++)
  341.     replace[k] = 0;
  342.       numelev++;
  343.     }
  344.   }
  345. }
  346.  
  347.  
  348. void upelev (void)
  349. {
  350.   short ii, jj;
  351.   for  (ii = 0; ii < numelev; ii++)
  352.   {
  353.     if ((elev[ii].origx >= (munchobj[YOU].x / 16) - 1)
  354.        && (elev[ii].curheight >= (munchobj[YOU].y / 16) - 1)
  355.        && (elev[ii].origx <= (munchobj[YOU].x / 16) + 1)
  356.        && (elev[ii].curheight <= (munchobj[YOU].y / 16) + 1)
  357.        && ((elevup == - 1) | (elevup == ii))
  358.        && (munchobj[YOU].y > 16))
  359.     {
  360.       checkhead (1);
  361.       if ((head1 < 50) && (head2 < 50))
  362.       {
  363.     replace[elev[ii].curheight - 1] = wgetworldblock (MAINWIN, elev[ii].origx * 16, (elev[ii].curheight - 1) * 16);
  364.     wputworldblock (MAINWIN, elev[ii].origx * 16, elev[ii].curheight * 16, 104);
  365.     wputworldblock (MAINWIN, elev[ii].origx * 16, (elev[ii].curheight - 1)*16, 105);
  366.     elev[ii].curheight--;
  367.     elevup = ii;
  368.     munchobj[YOU].y -= 16;
  369.     elev[ii].timer = 10;
  370.       }
  371.     }
  372.   }
  373. }
  374.  
  375.  
  376. void downelev (void)
  377. {
  378.   short ii, jj;
  379.   for  (ii = 0; ii < numelev; ii++)
  380.   {
  381.     if  ((elev[ii].origx >= (munchobj[YOU].x / 16) - 1)
  382.     && (elev[ii].curheight >= (munchobj[YOU].y / 16) - 1)
  383.     && (elev[ii].origx <= (munchobj[YOU].x / 16) + 1)
  384.     && (elev[ii].curheight <= (munchobj[YOU].y / 16) + 1)
  385.     && (elev[ii].curheight != elev[ii].origy))
  386.     {
  387.       wputworldblock (MAINWIN, elev[ii].origx * 16, elev[ii].curheight * 16, replace[elev[ii].curheight]);
  388.       wputworldblock (MAINWIN, elev[ii].origx * 16, (elev[ii].curheight + 1) * 16, 105);
  389.       elev[ii].curheight++;
  390.       if (elev[ii].curheight == elev[ii].origy)
  391.     elevup = - 1;
  392.       munchobj[YOU].y += 16;
  393.       elev[ii].timer = 10;
  394.     }
  395.   }
  396. }
  397.  
  398.  
  399. void checkelevators (void)
  400. {
  401.   short ii;
  402.   
  403.   for  (ii = 0; ii < numelev; ii++)
  404.   {
  405.     if  ((elev[ii].curheight != elev[ii].origy))
  406.     {
  407.       if  (elev[ii].timer == 0)
  408.       {
  409.     wputworldblock (MAINWIN, elev[ii].origx * 16, elev[ii].curheight * 16, replace[elev[ii].curheight]);
  410.     wputworldblock (MAINWIN, elev[ii].origx * 16, (elev[ii].curheight + 1) * 16, 105);
  411.     elev[ii].curheight++;
  412.     if (elev[ii].curheight == elev[ii].origy)
  413.       elevup = - 1;
  414.     elev[ii].timer = 0;
  415.       }
  416.       else elev[ii].timer--;
  417.     }
  418.   }
  419. }
  420.  
  421.  
  422. void moveguys (void)
  423. {
  424.   short j, k;
  425.   
  426.   for (i = 0; i <= 36; i++)
  427.   {
  428.     if (munchobj[i].on == 1) 
  429.       if (munchsprites[munchobj[i].num] != NULL)
  430.     // sprite made
  431.     {
  432.       if (is_in_window (MAINWIN, munchobj[i].x, munchobj[i].y, 100))
  433.       {
  434.     if (munchobj[i].num < 16)     // walking right
  435.     {
  436.       munchobj[i].num++;
  437.       if (munchobj[i].num > 15) munchobj[i].num = 12;
  438.       // walking animation loop
  439.       munchobj[i].x += 3;
  440.       j = wgetworldblock (MAINWIN, munchobj[i].x + 16, munchobj[i].y + 16);
  441.       k = wgetworldblock (MAINWIN, munchobj[i].x + 16, munchobj[i].y + 8);
  442.       if ((j < 50) | (k >= 50)) munchobj[i].num = 16;
  443.     }
  444.     if (munchobj[i].num > 15)     // walking left
  445.     {
  446.       munchobj[i].num++;
  447.       if  (munchobj[i].num > 19) munchobj[i].num = 16;
  448.       // walking animation loop
  449.       munchobj[i].x -= 3;
  450.       j = wgetworldblock (MAINWIN, munchobj[i].x, munchobj[i].y + 16);
  451.       k = wgetworldblock (MAINWIN, munchobj[i].x, munchobj[i].y + 8);
  452.       if ((j < 50) || (k >= 50)) munchobj[i].num = 12;
  453.     }
  454.       }
  455.     }
  456.   }
  457. }
  458.